package com.example.service.impl;

import com.example.pojo.Privilege;
import com.example.mapper.PrivilegeMapper;
import com.example.service.PrivilegeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-20
 */
@Service
public class PrivilegeServiceImpl extends ServiceImpl<PrivilegeMapper, Privilege> implements PrivilegeService {
    @Autowired
    private PrivilegeMapper privilegeMapper;

    @Override
    public List<Privilege> selectRootPrivilege() {
        //先查询目录根元素
        List<Privilege> rootPrivileges = privilegeMapper.selectRootPrivilege();

        for (Privilege rootPrivilege : rootPrivileges) {
            List<Privilege> children = privilegeMapper.selectWithChildren(rootPrivilege.getPid());
            rootPrivilege.setChildren(children);
        }

        return rootPrivileges;
    }

    @Override
    public List<Privilege> selectByParentId(Integer parentId) {
        return privilegeMapper.selectByParentId(parentId);
    }


}
